package com.honeyms.project.system.usernotice.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.honeyms.framework.aspectj.lang.annotation.Log;
import com.honeyms.framework.aspectj.lang.enums.BusinessType;
import com.honeyms.project.system.notice.domain.Notice;
import com.honeyms.project.system.notice.service.INoticeService;
import com.honeyms.project.system.usernotice.domain.MyNotice;
import com.honeyms.project.system.usernotice.domain.UserNotice;
import com.honeyms.project.system.usernotice.service.IUserNoticeService;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.framework.web.domain.AjaxResult;
import com.honeyms.common.utils.poi.ExcelUtil;
import com.honeyms.framework.web.page.TableDataInfo;

/**
 * 用户通知关联Controller
 * 
 * @author honeyms
 * @date 2020-11-25
 */
@Controller
@RequestMapping("/system/usernotice")
public class UserNoticeController extends BaseController
{
    private String prefix = "system/usernotice";

    @Autowired
    private IUserNoticeService userNoticeService;
    
    @Autowired
    private INoticeService noticeService;

    @RequiresPermissions("system:usernotice:view")
    @GetMapping()
    public String usernotice()
    {
        return prefix + "/usernotice";
    }
    
    @RequiresPermissions("system:usernotice:mynotice")
    @GetMapping("/mynotice")
    public String mynotice()
    {
    	return prefix + "/mynotice";
    }
    
    @RequiresPermissions("system:notice:view")
    @GetMapping("/readetail/{noticeId}")
    public String readetail(@PathVariable("noticeId") String noticeId, ModelMap mmap)
    {
    	mmap.put("noticeId", noticeId);
    	return prefix + "/usernotice";
    }

    /**
     * 查询用户通知关联列表
     */
    @RequiresPermissions("system:usernotice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserNotice userNotice)
    {
        startPage();
        List<UserNotice> list = userNoticeService.selectUserNoticeList(userNotice);
        return getDataTable(list);
    }
    
    /**
     * 查询我的通知列表
     */
    @RequiresPermissions("system:usernotice:mynotice")
    @PostMapping("/mylist")
    @ResponseBody
    public TableDataInfo mylist(MyNotice myNotice)
    {
    	startPage();
    	myNotice.setUserId(getUserId());
    	List<MyNotice> list = userNoticeService.selectMyNoticeList(myNotice);
    	return getDataTable(list);
    }
    
    /**
     * 查询我的通知列表
     */
    @RequiresPermissions("system:usernotice:mynotice")
    @GetMapping("/read/{id}")
    public String read(@PathVariable("id") Long id, ModelMap mmap)
    {
    	Notice notice = noticeService.selectNoticeById(id);
        UserNotice userNotice = userNoticeService.selectUserNoticeByusernoticeId(getUserId(),id);
        if (userNotice!=null) {
        	if (userNotice.getIsRead()==0) {
    			userNotice.setIsRead(1);
    			userNotice.setReadDate(new Date());
    			userNoticeService.updateUserNotice(userNotice);
    		}
		}else {
			notice=new Notice();
			notice.setNoticeTitle("错误通知!!!!");
			notice.setNoticeContent("<h1 style='color:red'>当前通知可能已被删除!请刷新或重新打开!!!<h1>");
			notice.setRemark("当前通知已被删除!");
		}
        mmap.put("notice", notice);
        return prefix + "/read";
    }
    
    /**
     * 查询我的未读通知列表
     */
    @GetMapping("/noread")
    @ResponseBody
    public List<MyNotice> noread(ModelMap mmap)
    {
    	MyNotice myNotice = new MyNotice();
    	myNotice.setUserId(getUserId());
    	myNotice.setIsRead(0);
        List<MyNotice> list = userNoticeService.selectMyNoticeList(myNotice);
        return list;
    }
    
    /**
     * 导出用户通知关联列表
     */
    @RequiresPermissions("system:usernotice:export")
    @Log(title = "用户通知关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserNotice userNotice)
    {
        List<UserNotice> list = userNoticeService.selectUserNoticeList(userNotice);
        ExcelUtil<UserNotice> util = new ExcelUtil<UserNotice>(UserNotice.class);
        return util.exportExcel(list, "usernotice");
    }

    /**
     * 新增用户通知关联
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户通知关联
     */
    @RequiresPermissions("system:usernotice:add")
    @Log(title = "用户通知关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserNotice userNotice)
    {
        return toAjax(userNoticeService.insertUserNotice(userNotice));
    }

    /**
     * 修改用户通知关联
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        UserNotice userNotice = userNoticeService.selectUserNoticeById(id);
        mmap.put("userNotice", userNotice);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户通知关联
     */
    @RequiresPermissions("system:usernotice:edit")
    @Log(title = "用户通知关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserNotice userNotice)
    {
        return toAjax(userNoticeService.updateUserNotice(userNotice));
    }

    /**
     * 删除用户通知关联
     */
    @RequiresPermissions("system:usernotice:remove")
    @Log(title = "用户通知关联", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userNoticeService.deleteUserNoticeByIds(ids));
    }
}
